package cs2340.todolist.controller.account_activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import cs2340.todolist.R;
import cs2340.todolist.application.ToDoListApplication;
import cs2340.todolist.controller.HomeScreenActivity;
import cs2340.todolist.model.account_management.AccountManagementMessageHandler;
import cs2340.todolist.model.account_management.AccountManager;
import cs2340.todolist.model.account_management.AccountManager.AccountManagementMessage;

/**
 * shows the login screen and display messages associated with logging in
 * @author BaoTam
 *
 */
public class LoginScreenActivity extends Activity {
	
	private AccountManager accountManager;
	
    /** Called when the loginscreen is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_screen);
        
    }
    
    /**
     * refresh its accountManager on start
     */
    public void onStart() {
    	super.onStart();
    	
        ((ToDoListApplication)getApplication()).getSessionManager().createAccountManager(this);
        accountManager = ((ToDoListApplication)getApplication()).getSessionManager().getAccountManager();
        accountManager.changeContext(this);
        
        final AccountManagementMessageHandler accountManagementMessageHandler = new LoginMessageHandler(this);
    	//gets text fields from GUI into a variable
        final EditText editUsername = (EditText)findViewById(R.id.edit_login_username);
        final EditText editPassword = (EditText)findViewById(R.id.edit_login_password);
        
        final Button loginButton = (Button) findViewById(R.id.login_button);
        loginButton.setOnClickListener(new OnClickListener(){
			@Override
			//called when the login button is pressed
			public void onClick(View arg0) {
				
				AccountManagementMessage loginMessage = accountManager.tryToLogin(
						editUsername.getText().toString(),
						editPassword.getText().toString()
						);
				accountManagementMessageHandler.handleMessage(loginMessage);
								
			}
        });        
        
        
        final Button createAccountButton = (Button) findViewById(R.id.create_account_button);
        createAccountButton.setOnClickListener(new OnClickListener(){
			@Override
			//called when the create account button is pressed
			public void onClick(View arg0) {
				//these two lines bring up the CreateAccountActivity activity
				Intent goToCreateAccount = new Intent(LoginScreenActivity.this,CreateAccountActivity.class);
				startActivity(goToCreateAccount);
			}
        });
    	
    }
    
    /**
     * handle incoming message from AccountManager
     * @author BaoTam
     *
     */
    public class LoginMessageHandler extends AccountManagementMessageHandler {
    	
    	/**
    	 * instantiate a message handler
    	 * @param context
    	 */
    	private LoginMessageHandler(Context context) {
    		super(context);
    	}
    	
    	/**
    	 * display a dialog with the given message and start the home screen if the message says that
    	 * login was successful
    	 * @param message
    	 */
    	public void handleMessage(AccountManagementMessage message) {
    		final AccountManagementMessage currentMessage = message;
    		//go to home screen
    		if(currentMessage.getMessage().equals(AccountManagementMessage.LOGIN_SUCCESSFUL_MESSAGE)) {
         	   ((ToDoListApplication)LoginScreenActivity.this.getApplication()).getSessionManager().setLoggedIn(true);
         	   Intent goToHomeScreen = new Intent(LoginScreenActivity.this,HomeScreenActivity.class);
         	   LoginScreenActivity.this.startActivity(goToHomeScreen);
            }
    		else {
	    		AlertDialog.Builder builder = new AlertDialog.Builder(context);
	    		builder.setMessage(message.getMessage())
	    		       .setCancelable(false)
	    		       .setNeutralButton("OK", new DialogInterface.OnClickListener() {
	    		           public void onClick(DialogInterface dialog, int id) {
	    		               dialog.cancel();
	    		           }
	    		       });
	    		AlertDialog alert = builder.create();
	    		alert.show();
    		}
    	}
    }
}
